//
//  TestThreeViewController.m
//  TestConstriant
//
//  Created by apple on 2018/7/6.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TestThreeViewController.h"
#import "TestThreeTableViewCell.h"
#import "TestThreeModel.h"

@interface TestThreeViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIPickerView *sexPickerView;

@property (strong, nonatomic) IBOutlet UIPickerView *rolePickerView;

@property(nonatomic,retain) NSMutableArray *pickDataArray;


@property(nonatomic,retain)NSMutableArray *dataArray;


@property(nonatomic,retain)NSIndexPath *selectIndexPath;

@end

@implementation TestThreeViewController

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self saveHistory];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [NSMutableArray array];
    
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TestThreeTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell_testThree"];
    
    [self getHistory];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TestThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_testThree" forIndexPath:indexPath];
    cell.nameTextField.tag = indexPath.row;
    cell.sexTextField.tag = indexPath.row;
    cell.roleTextField.tag = indexPath.row;
    cell.rankTextField.tag = indexPath.row;
    
    [cell.sexTextField addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [cell.sexTextField addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
    [cell.roleTextField addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [cell.roleTextField addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
    
    [cell.nameTextField addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [cell.rankTextField addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    
    cell.addButton.tag = indexPath.row;
    [cell.addButton addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside];
    
    TestThreeModel *model = self.dataArray[indexPath.row];
    cell.nameTextField.text =  model.name;
    cell.sexTextField.text = model.sex;
    cell.roleTextField.text = model.role;
    cell.rankTextField.text = model.rank;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
}

-(void)addButton:(UIButton *)button{
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"是否添加？" message:nil preferredStyle:UIAlertControllerStyleAlert] ;
    [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alertVc addAction:[UIAlertAction actionWithTitle:@"添加" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        TestThreeModel *model = [[TestThreeModel alloc] init];
        [self.dataArray addObject:model];
        [self.tableView reloadData];
    }]];
    
    [self.navigationController presentViewController:alertVc animated:NO completion:nil];
}
    

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    if (self.selectIndexPath) {
        [self saveDataToModelWithIndexpath:self.selectIndexPath];
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    self.selectIndexPath = indexPath;
    TestThreeTableViewCell *selectCell = [self.tableView  cellForRowAtIndexPath:indexPath];
   
    if (textField == selectCell.sexTextField) {
        
        NSString * file =  [[NSBundle mainBundle] pathForResource:@"sex" ofType:@"plist"];
        self.pickDataArray = [NSMutableArray arrayWithContentsOfFile:file];
        textField.inputView = self.sexPickerView;
    }
    if (textField == selectCell.roleTextField) {
        
        NSString * file =  [[NSBundle mainBundle] pathForResource:@"role" ofType:@"plist"];
        self.pickDataArray = [NSMutableArray arrayWithContentsOfFile:file];
        textField.inputView = self.rolePickerView;
    }
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    TestThreeTableViewCell *selectCell = [self.tableView  cellForRowAtIndexPath:indexPath];
    if (textField == selectCell.sexTextField) {
        
        NSInteger index = [self.sexPickerView selectedRowInComponent:0];
        textField.text = self.pickDataArray[index][@"content"];
        
    }
    if (textField == selectCell.roleTextField) {
    
        NSInteger index = [self.rolePickerView selectedRowInComponent:0];
        textField.text = self.pickDataArray[index][@"content"];
        
    }
    
}

-(void)saveDataToModelWithIndexpath:(NSIndexPath *)indexPath{
    
    TestThreeTableViewCell *selectCell = [self.tableView  cellForRowAtIndexPath:indexPath];
    TestThreeModel *model = self.dataArray[indexPath.row];
    model.name = selectCell.nameTextField.text;
    model.sex = selectCell.sexTextField.text;
    model.role = selectCell.roleTextField.text;
    model.rank = selectCell.rankTextField.text;
}

#pragma mark -UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.pickDataArray.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.pickDataArray[row][@"content"];
}


-(void)getHistory{
    
    NSArray *arr = [TestThreeModel findAll];
    self.dataArray = [arr mutableCopy];
    if (self.dataArray.count == 0) {
        TestThreeModel *model = [[TestThreeModel alloc] init];
        [self.dataArray addObject:model];
    }
    
    [self.tableView reloadData];

}


-(void)saveHistory{
    //
    [self saveDataToModelWithIndexpath:self.selectIndexPath];
    
    if ([TestThreeModel isExistInTable]) {
        
        for (TestThreeModel *model in self.dataArray) {
            [model saveOrUpdate];
        }
    }else{
        
        [TestThreeModel createTable];
        [TestThreeModel saveObjects:self.dataArray];
    }
    
}

@end
